package com.njzxw.fs.serversys.repository;


import com.njzxw.fs.commonutil.db.ZxwLangDriver;
import com.njzxw.fs.serversys.bean.UserBean;
import com.njzxw.fs.serversys.bean.WeiXinBean;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserReq {

    @Select("select a.id,a.state,(case when a.state = 1 then '正常' else '禁用' end) state_name,a.`name`,a.mobile,a.headimg,CONCAT(j.img_host,j.file_name) head_img,a.email,DATE_FORMAT(a.create_time,'%Y-%m-%d %T') create_time," +
            "DATE_FORMAT(a.bir,'%Y-%m-%d') bir,a.addr,GROUP_CONCAT(c.role_name) role_name,\n" +
            "\t\t\tGROUP_CONCAT(f.org_name) org_name,GROUP_CONCAT(h.org_name) org_manage_name,GROUP_CONCAT(h.id) org_manage_id\n" +
            "  from t_manager_user a left join t_resouce_file j on j.id = a.headimg left join t_manager_user_role b on a.id = b.manager_user_id LEFT join " +
            "t_manager_role c on c.id = b.manager_role_id\n" +
            "\t     left join t_manager_user_org e on a.id = e.manager_user_id LEFT join t_manager_org f on f.id = e.manager_org_id " +
            "\t     LEFT join t_manager_user_orgmanage g on a.id = g.manager_user_id LEFT join t_manager_org h on h.id = g.manager_org_id " +
            " where 1=1 and a.name like #{name} and a.email like #{email} and a.mobile like #{mobile} \n" +
            "\t\t\tgroup by a.id,a.state,a.`name`,a.mobile,a.headimg,a.email,create_time,a.bir,a.addr ")
    @Lang(ZxwLangDriver.class)
    List<Map<String,Object>> getListPageManageOrg(UserBean userBean);

    @Select("select DATE_FORMAT(a.bir,'%Y-%m-%d') bir,a.addr,a.id,a.state,a.`name`,a.mobile,a.headimg,a.headimg headid,CONCAT(j.img_host,j.file_name) head_img,a.email,DATE_FORMAT(a.create_time,'%Y-%m-%d %T') create_time," +
            "GROUP_CONCAT(c.role_name) role_name,GROUP_CONCAT(c.id) role_id,GROUP_CONCAT(f.id) org_id,\n" +
            "\t\t\tGROUP_CONCAT(f.org_name) org_name,GROUP_CONCAT(h.org_name) org_manage_name,GROUP_CONCAT(h.id) org_manage_id \n" +
            "  from t_manager_user a left join t_resouce_file j on j.id = a.headimg left join t_manager_user_role b on a.id = b.manager_user_id LEFT join " +
            "   t_manager_role c on c.id = b.manager_role_id\n" +
            "\t     left join t_manager_user_org e on a.id = e.manager_user_id " +
            "LEFT join t_manager_org f on f.id = e.manager_org_id \n" +
            "LEFT join t_manager_user_orgmanage g on a.id = g.manager_user_id LEFT join t_manager_org h on h.id = g.manager_org_id  where a.id = #{id}\n" +
            "\t\t\tgroup by a.state,a.`name`,a.mobile,a.headimg,a.email,create_time,bir,a.addr ")
    @Lang(ZxwLangDriver.class)
    Map<String,Object> getMap(UserBean userBean);

    @Insert("insert into t_manager_user(#{userBean})")
    @Lang(ZxwLangDriver.class)
    @Options(useGeneratedKeys = true)
    int insertUser(UserBean userBean);

    @Insert("insert into t_manager_user_org(manager_user_id,manager_org_id)values(#{id},#{org_id})")
    int insertUserOrg(UserBean userBean);

    @Insert("insert into t_manager_user_orgmanage(manager_user_id,manager_org_id)values(#{id},#{org_id})")
    int insertUserOrgManager(UserBean userBean);

    @Update("update t_manager_user(#{userBean}) where id = #{id}")
    @Lang(ZxwLangDriver.class)
    Integer updateUser(UserBean userBean);

    @Insert("insert into t_manager_user_role(manager_user_id,manager_role_id)values(#{id},#{role_id})")
    int insertUserRole(UserBean userBean);

    @Delete("delete from t_manager_user_org where manager_user_id = #{id}")
    int deleteUserOrg(UserBean userBean);

    @Delete("delete from t_manager_user_orgmanage where manager_user_id = #{id}")
    int deleteUserOrgManager(UserBean userBean);

    @Delete("delete from t_manager_user_role where manager_user_id = #{id}")
    int deleteUserRule(UserBean userBean);

    @Delete("delete from t_manager_user where id = #{id}")
    int deleteUser(UserBean userBean);

    @Update("update t_manager_user a set a.password = #{password} where a.id = #{id}")
    int resetPassword(UserBean userBean);

    @Select("select a.state" +
            " from t_manager_user a where a.password = #{password} and a.merchant_id = #{merchant_id} and (a.mobile = #{mobile} or a.email = #{email}) ")
    @Lang(ZxwLangDriver.class)
    Integer getUserState(UserBean userBean);

    @Update("update t_manager_user a set a.state = (case when a.state = 0 then 1 else 0 end) where a.id = #{id}")
    int forbidden(UserBean userBean);

    @Select("select * from t_manager_role a where a.state = 1")
    List<Map<String,Object>> getRoleList();

    @Select("select count(1) from t_manager_user a where a.mobile = #{mobile} and a.id != #{id}")
    @Lang(ZxwLangDriver.class)
    int checkUserMobile(UserBean userBean);

    @Select("select count(1) from t_manager_user a where a.email = #{email} and a.id != #{id}")
    @Lang(ZxwLangDriver.class)
    int checkUserEmail(UserBean userBean);

    @Select("select a.terrace,DATE_FORMAT(a.bir,'%Y-%m-%d') bir,a.addr,a.id,a.state,a.`name`,a.mobile,a.headimg,a.headimg headid,CONCAT(j.img_host,j.file_name) head_img,a.email,DATE_FORMAT(a.create_time,'%Y-%m-%d %T') create_time," +
            "GROUP_CONCAT(c.role_name) role_name,GROUP_CONCAT(c.id) role_id,GROUP_CONCAT(f.id) org_id,\n" +
            "\t\t\tGROUP_CONCAT(f.org_name) org_name\n" +
            "  from t_manager_user a left join t_resouce_file j on j.id = a.headimg left join t_manager_user_role b on a.id = b.manager_user_id LEFT join " +
            "   t_manager_role c on c.id = b.manager_role_id\n" +
            "\t     left join t_manager_user_org e on a.id = e.manager_user_id LEFT join t_manager_org f on f.id = e" +
            ".manager_org_id where a.password = #{password} and (a.mobile = #{mobile} or a.email = #{email}) \n" +
            "\t\t\tgroup by a.state,a.`name`,a.mobile,a.headimg,a.email,create_time,bir,a.addr ")
    @Lang(ZxwLangDriver.class)
    Map<String,Object> getMapPass(UserBean userBean);

    @Select("select CONCAT(GROUP_CONCAT(b.next_org_ids),GROUP_CONCAT(b.id),',') from t_manager_user_orgmanage a,t_manager_org b " +
            " where a.manager_org_id = b.id and a.manager_user_id = #{id}")
    String getManageOrg(UserBean userBean);

    /**
     * 获取用户对应权限的url链接
     * @param userBean
     * @return
     */
    @Select("select e.url,md5(e.url) urlmd5,e.`name`,f.menu_name from t_manager_user a,t_manager_user_role b,t_manager_role_authority " +
            "c,t_manager_menu_authority e,t_manager_menu f  \n" +
            "where a.id = b.manager_user_id\n" +
            "  and b.manager_role_id = c.role_id\n" +
            "  and c.authority_id = e.id\n" +
            "\tand e.menu_id = f.id \n" +
            "\tand (a.mobile = #{mobile} or a.email = #{email}) \n" +
            "\tand e.url is not null")
    List<Map<String,Object>> getUserMenuUrl(UserBean userBean);

    @Select("${sql}")
    List<Map<String,Object>> getTmpList(@Param("sql") String sql);

    @Insert("${sql}")
    Integer insert(@Param("sql") String sql);

    @Update("${sql}")
    Integer update(@Param("sql") String sql);

    @Insert("insert into t_user_info(wexi_id,uuid)values(#{wexi_id},#{uuid})")
    @Options(useGeneratedKeys = true)
    Integer insertWxUser(WeiXinBean weiXinBean);

    @Select("select count(1) from t_user_info a where a.wexi_id = #{wexi_id}")
    Integer checkWxOperIdUser(WeiXinBean weiXinBean);

    @Update("update t_user_info a set a.uuid = #{uuid} where a.wexi_id = #{wexi_id} ")
    Integer updateWxUser(WeiXinBean weiXinBean);

    @Select("select id from t_user_info a where a.wexi_id = #{wexi_id}")
    Integer selectUserId(WeiXinBean weiXinBean);

    @Select("select count(1) from t_user_info a where a.wexi_id = #{wexi_id} and a.phone is not null ")
    Integer checkWxMobile(WeiXinBean weiXinBean);

    @Select("select a.* from t_user_info a where a.id = #{oper_id}")
    Map<String,Object> getWeiXinInfo(WeiXinBean weiXinBean);

    @Select("select a.* from t_user_info a where a.id = #{oper_id}")
    Integer checkPassWord(UserBean userBean);
}
